using Microsoft.SharePoint;

namespace Ratman.SharePoint.SharePointSandBoxMediaPlayer
{
    public class SharePointSandBoxMediaPlayerWebPartFeatureReceiver : SPFeatureReceiver
    {

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPSite _SPSite = properties.Feature.Parent as SPSite)
            {
                using (SPWeb _SPWeb = _SPSite.RootWeb)
                {
                    SPList _SPList = _SPWeb.Lists["Web Part Gallery"];

                    for (int i = _SPList.ItemCount - 1; i >= 0; i--)
                    {
                        string webpartName = _SPList.Items[i].Name;
                        webpartName = webpartName.Substring(0, webpartName.IndexOf('.')) + "WebPart";

                        if (properties.Feature.Definition.DisplayName == webpartName)
                        {
                            _SPList.Items[i].Delete();
                            break;
                        }
                    }
                }
            }
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
